const puppeteer = require('puppeteer');
const request = require('request');
const xlsx = require('node-xlsx');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM(`<!DOCTYPE html>`);
const $ = require('jQuery')(window);

// console.log($) //查看jquery是否能够执行

var sheets = xlsx.parse('./1119.xlsx');//获取到所有sheets
var data = sheets[1].data;
var students = [];
var loginUrl = 'https://back.tzedu.net.cn/login'

data.forEach(function (data) {
    let i = [
        data[2], data[4], data[5]
    ]
    students.push(i)
});
// console.log(students[1][1])
async function go() {
    let browser = await puppeteer.launch({
        headless: false,         //开启视图
        defaultViewport: null,
    });
    let page = await browser.newPage();
    console.log('······打开列表页面······')
    await page.goto(loginUrl);
    await login(page)

    let tabdcl = await page.$x('//*[@id="app"]/div/main/div/div/div[1]/div[1]/label[2]')
    await tabdcl[0].click()
    var nameInput = await page.$x('//*[@id="app"]/div/main/div/div/div[1]/div[2]/div[2]/input') // 此处使用 xpath 寻找控件，下同
    var searchBut = await page.$x('//*[@id="app"]/div/main/div/div/div[1]/div[2]/button') // 此处使用 xpath 寻找控件，下同
    // 0 名字  1 年纪  2  班级
    for (let i = 1; i < students.length; i++) {
        await nameInput[0].type(students[i][0])
        await searchBut[0].click()
        await page.waitFor(2000)
        await searchBut[0].click()
        await autoScroll(page);
        await page.waitFor(1000)
        await page.evaluate(() => document.getElementsByClassName('el-input__inner')[1].value = "")
    }

}
go()




//查询
function autoScroll(page) {
    // console.log(age)
    return page.evaluate(() => {
        return new Promise((resolve) => {

            var btn = document.getElementsByClassName("el-button--primary")[1]
            btn.addEventListener('click',function(){
                resolve()

            },false)
    
        })
    });
}

//登录
async function login(page) {
    let tabone = await page.$x('//*[@id="tab-first"]')
    await tabone[0].click()
    let accountElements = await page.$x('//*[@id="pane-first"]/form/div[1]/div/div[1]/input') // 此处使用 xpath 寻找控件，下同
    console.log('<-----------// 2. 填入账号----------->')
    await accountElements[0].type('')
    console.log('<-----------// 3. 选中密码控件----------->')
    let pwdElements = await page.$x('//*[@id="pane-first"]/form/div[2]/div/div/input')
    console.log('<-----------// 4. 填入密码----------->')
    await pwdElements[0].type('')
    console.log('<-----------// 5. 点击登录----------->')
    let loginButtons = await page.$x('//*[@id="pane-first"]/form/div[3]/div/button')
    await loginButtons[0].click()
    await page.waitFor(1000)
    await page.goto('https://back.tzedu.net.cn/unusual');
}
